##
rm(list=ls())
gc()


library(data.table)
library(ggplot2)
library(dplyr)
library(stringr)

files = paste0('results/',c("current-results-2008-2012-CA.Rdata",
                                        "current-results-2008-2012-KS.Rdata",
                                        "current-results-2008-2012-NC.Rdata",
                                        "current-results-2008-2012-NY.Rdata",
                                        "current-results-2008-2012-FL.Rdata",
  
  
  
                                        "current-results-2012-2016-CA.Rdata",
                                        "current-results-2012-2016-KS.Rdata",
                                        "current-results-2012-2016-NC.Rdata",
                                        "current-results-2012-2016-NY.Rdata",
                                        "current-results-2012-2016-FL.Rdata",
                                        
                                        "current-results-2016-2020-CA.Rdata",
                                        "current-results-2016-2020-KS.Rdata",
                                        "current-results-2016-2020-NC.Rdata",
                                        "current-results-2016-2020-NY.Rdata",
                                        "current-results-2016-2020-FL.Rdata"
  
                                        ))




results = rbindlist(lapply(files, FUN=function(file){

  
  load(file)
  
  models = ls()[grepl('Model',ls())]
  
  l = rbindlist(lapply(models, FUN=function(x){
   # print(x)
    m = get(x)
    out = as.data.table(m$coefficient)
    out[,Covariate:=rownames(m$coefficient)]
    out[,Model:=x]
    out[,N:=m$N]
    out[,`R-Squared`:=m$r.squared]
    out[,`Adjusted R-Squared`:=m$adj.r.squared]
    out[,Sigma:=m$sigma]
    out[,FStat:=m$fstat]
    if('Cluster s.e.'%in%names(out)){
      out[,SE:=`Cluster s.e.`]
      out[,SE.type:='Cluster']
      out[,`Cluster s.e.`:=NULL]
    } else {
      out[,SE:=`Robust s.e`]
      out[,SE.type:='Robust']
      out[,`Robust s.e`:=NULL]
    }


return(out)

  }))
  
  l[grepl('2008',file),Year1:='2008']
  l[grepl('2008',file),Year2:='2012']
l[grepl('2020',file),Year1:='2016']
l[grepl('2020',file),Year2:='2020'] 
l[!grepl('2008',file)&!grepl('2020',file),Year1:='2012']
l[!grepl('2008',file)&!grepl('2020',file),Year2:='2016']
l[,file:=file]

return(l)
}))


results = results[Covariate %in% c('DemSpExpDiff_nohh','RepSpExpDiff_nohh')]

results[grepl('-CA',file),State:='CA']
results[grepl('-NY',file),State:='NY']
results[grepl('-FL',file),State:='FL']
results[grepl('-NC',file),State:='NC']
results[grepl('-KS',file),State:='KS']

results[grepl('DemSpExp',Model),Exposure.Type:='Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration']
results[grepl('RepSpExp',Model),Exposure.Type:='Effect of\nexposure to\nRepublicans on\nRepublican\nregistration']

results[grepl('Dems',Model),Subset:='Democrats']
results[grepl('Reps',Model),Subset:='Republicans']
results[grepl('Oths',Model),Subset:='Non-partisans']
results[,Years:=paste0(Year1,'-',Year2)]

colors = c(Democrats = "#377EB8", Republicans = "#E41A1C", `Non-partisans` = "purple")


g1 = ggplot(results[State=='CA' ], aes(y = Estimate, x = Subset, color = Subset))+
  geom_point(size = 5, position = position_dodge(width = 0.75))+
  geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
  geom_hline(yintercept = 0, linetype = 'dashed')+
  coord_flip()+
  theme_bw()+
  scale_color_manual(values = colors)+
  ylab('Coefficient on change in partisan exposure')+
  xlab('Original partisan registration')+
  guides(color = 'none')+
  theme(text = element_text(size = 20, family = 'serif'), legend.title=element_blank(),strip.text.y = element_text(size=12,angle=0),legend.position = 'bottom')+
  facet_grid(Exposure.Type~Years)

ggsave(plot = g1,filename = 'figures/FigS10a.png',dpi=300, width = 11, height = 6, units = 'in')


g2 = ggplot(results[State=='FL' ], aes(y = Estimate, x = Subset, color = Subset))+
  geom_point(size = 5, position = position_dodge(width = 0.75))+
  geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
  geom_hline(yintercept = 0, linetype = 'dashed')+
  coord_flip()+
  theme_bw()+
  scale_color_manual(values = colors)+
  ylab('Coefficient on change in partisan exposure')+
  xlab('Original partisan registration')+
  guides(color = 'none')+
  theme(text = element_text(size = 20, family = 'serif'), legend.title=element_blank(),strip.text.y = element_text(size=12,angle=0),legend.position = 'bottom')+
  facet_grid(Exposure.Type~Years)

ggsave(plot = g2,filename = 'figures/FigS10b.png',dpi=300, width = 11, height = 6, units = 'in')


g3 = ggplot(results[State=='NC' ], aes(y = Estimate, x = Subset, color = Subset))+
  geom_point(size = 5, position = position_dodge(width = 0.75))+
  geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
  geom_hline(yintercept = 0, linetype = 'dashed')+
  coord_flip()+
  theme_bw()+
  scale_color_manual(values = colors)+
  ylab('Coefficient on change in partisan exposure')+
  xlab('Original partisan registration')+
  guides(color = 'none')+
  theme(text = element_text(size = 20, family = 'serif'), legend.title=element_blank(),strip.text.y = element_text(size=12,angle=0),legend.position = 'bottom')+
  facet_grid(Exposure.Type~Years)

ggsave(plot = g3,filename = 'figures/FigS10d.png',dpi=300, width = 11, height = 6, units = 'in')

g4 = ggplot(results[State=='KS' ], aes(y = Estimate, x = Subset, color = Subset))+
  geom_point(size = 5, position = position_dodge(width = 0.75))+
  geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
  geom_hline(yintercept = 0, linetype = 'dashed')+
  coord_flip()+
  theme_bw()+
  scale_color_manual(values = colors)+
  ylab('Coefficient on change in partisan exposure')+
  xlab('Original partisan registration')+
  guides(color = 'none')+
  theme(text = element_text(size = 20, family = 'serif'), legend.title=element_blank(),strip.text.y = element_text(size=12,angle=0),legend.position = 'bottom')+
  facet_grid(Exposure.Type~Years)

ggsave(plot = g4,filename = 'figures/FigS10c.png',dpi=300, width = 11, height = 6, units = 'in')


g5 = ggplot(results[State=='NY' ], aes(y = Estimate, x = Subset, color = Subset))+
  geom_point(size = 5, position = position_dodge(width = 0.75))+
  geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
  geom_hline(yintercept = 0, linetype = 'dashed')+
  coord_flip()+
  theme_bw()+
  scale_color_manual(values = colors)+
  ylab('Coefficient on change in partisan exposure')+
  xlab('Original partisan registration')+
  guides(color = 'none')+
  theme(text = element_text(size = 20, family = 'serif'), legend.title=element_blank(),strip.text.y = element_text(size=12,angle=0),legend.position = 'bottom')+
  facet_grid(Exposure.Type~Years)

ggsave(plot = g5,filename = 'figures/FigS10e.png',dpi=300, width = 11, height = 6, units = 'in')
